package net.xo68.destiny.starter.manager.worker.route.impl;

import lombok.extern.slf4j.Slf4j;
import net.xo68.destiny.starter.manager.worker.route.IWorkerRouteStrategy;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 轮循 路由策略实现
 * @author wuxie
 * @version 2020-12-23
 */
@Slf4j
@Component("roundWorkerRouteStrategy")
public class RoundWorkerRouteStrategy implements IWorkerRouteStrategy {

    private final AtomicInteger seq=new AtomicInteger(0);

    @Override
    public String exec(List<String> hosts) {
        if(hosts==null || hosts.isEmpty()){
            return null;
        }
        int idx=seq.getAndIncrement();
        log.debug("round index:{}", idx);
        String addr= hosts.get(idx % hosts.size());
        return addr;
    }
}
